/*
 * Copyright (c) 2016. zhaojin 15398699939@163.com
 */

package com.bankscene.screenfit.vus;

import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.view.View;

import com.bankscene.screenfit.dict.ErrorDict;
import com.bankscene.screenfit.util.ViewUtils;

import org.greenrobot.eventbus.EventBus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import butterknife.Unbinder;

public abstract class BaseVu implements Vu {

    private Logger log = LoggerFactory.getLogger(BaseVu.class);

    /**
     * 获取当前View维护的主视图容器对象
     */
    protected View rootView;

    protected EventBus bus;

    protected Unbinder unbinder;

    private ViewUtils viewUtils;

    @Override
    public View getRootView() {
        return rootView;
    }

    @Override
    public void onUnwrap() {
        if (null != unbinder) {
            unbinder.unbind();
        } else {
            log.warn(ErrorDict.ERROR_UNBINDER_NOTBENULL.toString());
        }
    }

    /**
     * 获取当前View维护的主视图容器中的组件
     * 可以先考虑使用{@link butterknife.ButterKnife#findById}替代
     *
     * @param id
     * @param viewClazz
     * @param <T>
     * @return
     */
    @NonNull
    public <T extends View> T findViewBy(@NonNull @IdRes int id, @NonNull Class<T> viewClazz) {
        if (viewUtils == null) {
            viewUtils = new ViewUtils(rootView);
        }
        return viewUtils.findViewById(id, viewClazz);
    }

    @Override
    public void onFragmentBackPressed() {}
}
